package com.iot.inter.interfaceprocessing.newinterface;

import com.iot.inter.entity.common.DeviceFlowUsage;
import com.iot.inter.entity.newpojo.Constant;
import com.iot.inter.entity.newpojo.TurntoInterfaceUtils;
import com.iot.inter.entity.newpojo.request.RequestBody.PayNotifyRequestBody;
import com.iot.inter.entity.newpojo.request.RequestHeader;
import com.iot.inter.entity.newpojo.request.subRequest.PayNotifyRequest;
import com.iot.inter.entity.newpojo.response.subResponse.PayNotifyResponse;
import com.iot.inter.utils.HttpClientTool;
import com.iot.inter.utils.JaxbUtil;
import org.apache.log4j.Logger;

public class TrunToNoticyService {
    private static Logger logger = Logger.getLogger(TrunToNoticyService.class);

    /**
     * 去北京做：3.15支付结果通知:2.0接口
     * <p>
     * 接入方可以通过调用该接口，向业务网关同步某个套餐订单的支付结果。
     * 只有支付成功的时候，才需要调此接口进行通知。
     *
     * @author lm
     * @date 2019/8/5
     */
    public DeviceFlowUsage notify(String orderId, String deviceId, String payType, String payAmount, String secret, String partnerCode) {

        try {
            //请求body参数处理
            PayNotifyRequestBody body = new PayNotifyRequestBody();
            body.setOrderId(orderId);
            body.setDeviceId(deviceId);
            body.setPayType("1");
            body.setPayAmount(payAmount);

            if (partnerCode.equalsIgnoreCase(Constant.TRACKER_P000818) ||
                    partnerCode.equalsIgnoreCase(Constant.TRACKER_P000359) ||
                    partnerCode.equalsIgnoreCase(Constant.TRACKER_P000779) ||
                    partnerCode.equalsIgnoreCase(Constant.TRACKER_P000877) ||
                    partnerCode.equalsIgnoreCase(Constant.TRACKER_P000598)) {
                body.setIsOpen("1");
            }

            //参数拼接
            PayNotifyRequest request = new PayNotifyRequest();
            TurntoInterfaceUtils utils = new TurntoInterfaceUtils();
            RequestHeader header = utils.getRequestHeader(Constant.cmd_payNotify, partnerCode);
            request.setHeader(header);
            request.setBody(body);
            String sign = utils.getSign(header, body, body.getClass(), secret);
            request.setSign(sign);

            // 将参数转换成XML格式
            String sRequest = new JaxbUtil(PayNotifyRequest.class).toXml(request, "UTF-8");
            logger.info("【支付结果通知】xml请求：" + sRequest);
            // 发送post请求，并接收返回结果
            String result = HttpClientTool.sendHttpPost(Constant.BASE_URL, sRequest);
            logger.info("url=" + Constant.BASE_URL + ";接口名：" + Constant.cmd_payNotify);

            logger.info("【支付结果通知】xml响应：" + result);

            if (result != null) {

                PayNotifyResponse responseObj = new JaxbUtil(PayNotifyResponse.class).fromXml(result, "utf-8");
                // 返回错误结果的处理
                DeviceFlowUsage resultNotify = new DeviceFlowUsage();
                resultNotify.setRetCode(responseObj.getHeader().getResultCode());
                resultNotify.setResultInfo(responseObj.getHeader().getResultInfo());
                return resultNotify;
            } else {
                DeviceFlowUsage resultNotify = new DeviceFlowUsage();
                resultNotify.setRetCode("9999");
                resultNotify.setResultInfo("响应结果是null");
                return resultNotify;
            }

        } catch (Exception e) {
            DeviceFlowUsage resultNotify = new DeviceFlowUsage();
            resultNotify.setRetCode("9999");
            resultNotify.setResultInfo("响应结果异常");
            return resultNotify;
        }

    }
}
